package com.song.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.song.mapper.PlayerMapper;
import com.song.mapper.TeamMapper;
import com.song.pojo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 球员逻辑控制层
 */
@Service
public class PlayerService {
    @Resource
    private PlayerMapper playerMapper;
    @Resource
    private TeamService teamService;
    @Resource
    private TeamMapper teamMapper;

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public PageInfo<Player> queryPlayers(Integer pageNum, Integer pageSize, PlayerQueryVO vo){
        PlayerExample example = new PlayerExample();
        PlayerExample.Criteria criteria = example.createCriteria();
        if (pageNum == null || pageNum < 0){
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 0){
            pageSize = 5;
        }
        if (vo.getTeamName() != null&&!"".equals(vo.getTeamName().trim())){
            //再team中模糊查询，找到teamId
            List<Integer> list = new ArrayList<>();
            TeamQueryVO teamQueryVO = new TeamQueryVO();
            teamQueryVO.setChineseName(vo.getTeamName());
            PageInfo<Team> pageInfo = teamService.queryTeams(null, null, teamQueryVO);
            for (Team team : pageInfo.getList()) {
                list.add(team.getTeamId());
            }
            criteria.andTeamIdIn(list);
        }
        if (vo.getPlayerName() != null&&!"".equals(vo.getPlayerName().trim())){
            criteria.andPlayerNameLike("%"+vo.getPlayerName().trim()+"%");
        }
        if (vo.getPlayerNum() != null&& vo.getPlayerNum() >= 0){
            criteria.andPlayerNumEqualTo(vo.getPlayerNum());
        }
        if (vo.getLocation() != null&&!"".equals(vo.getLocation().trim())){
            criteria.andLocationLike("%"+vo.getLocation().trim()+"%");
        }
        if (vo.getBirthday() != null){
            criteria.andBirthdayEqualTo(vo.getBirthday());
        }
        if(vo.getNationality() != null&&!"".equals(vo.getNationality().trim())){
            criteria.andNationalityLike("%"+vo.getNationality().trim()+"%");
        }
        if (vo.getHeight() != null&& vo.getHeight() >= 0){
            criteria.andHeightGreaterThanOrEqualTo(vo.getHeight());
        }
        if (vo.getWeight() != null&& vo.getWeight() >= 0){
            criteria.andWeightGreaterThanOrEqualTo(vo.getWeight());
        }
        if (vo.getSalary() != null&& vo.getSalary() >= 0){
            criteria.andSalaryGreaterThanOrEqualTo(vo.getSalary());
        }
        if (vo.getStatus() != null&& vo.getStatus() != -1){
            criteria.andStatusEqualTo(vo.getStatus());
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Player> players = playerMapper.selectByExample(example);
        //List<Player> players = playerMapper.selectByExample(example);
        //查询球员所属球队名称
        for (Player player : players) {
            Team team = teamService.queryByTeamId(player.getTeamId());
            player.setTeam(team);
        }
        return new PageInfo<>(players);
    }

    /**
     * 新增
     * @param player
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer insertPlayer(Player player){
        return playerMapper.insertSelective(player);
    }

    /**
     * 通过主键查询
     * @param playerId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Player queryByPlayerId(Integer playerId){
        Player player = playerMapper.selectByPrimaryKey(playerId);
        //通过player中teamId查询team
        if (player != null){
            Team team = teamMapper.selectByPrimaryKey(player.getTeamId());
            player.setTeam(team);
            return player;
        }
        return null;
    }

    /**
     * 通过主键更新
     * @param player
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer updatePlayer(Player player){
       return playerMapper.updateByPrimaryKeySelective(player);
    }

    /**
     * 删除
     * @param playerId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer deletePlayer(Integer playerId){
        Player player = playerMapper.selectByPrimaryKey(playerId);
        if (player != null){
            player.setIsDel(1);
            return playerMapper.updateByPrimaryKeySelective(player);
        }
        return null;
    }
}
